range(n) generates the series of n values from 0 to n − 1
for i inrange(5):print(i)
0
1
2
3
4
# looping through data indices. find the maxB = [1, 4, 6, 7, 89, 54]big_indx =0for i inrange(len(B)):if B[i] > B[big_indx]: big_indx = iprint('The max value in B is', B[big_indx], 'found on position', big_indx)
The max value in B is 89 found on position 4
Index based for loops - enumerate()
assigns a count to each item within an iterable and returns it as an enumerate object
one way to avoid nested loops
import numpy as nparray_a = np.arange(20, 25)for indx, val inenumerate(array_a):print('the index is', indx)print('the value is', val)
the index is 0
the value is 20
the index is 1
the value is 21
the index is 2
the value is 22
the index is 3
the value is 23
the index is 4
the value is 24
! range() and enumerate() - none of the two returns a list of objects!
Break and continue statements
break - immediately terminates the loop
continue - skips whatever is after it and continues with the next iteration
mostly used after a conditional statement
While loops
Perform a task while something is True
Be careful:
Some loops never finish (get stuck)
Make sure that condition for ending the loop can be fullfilled